package com.twu.biblioteca.domain;

import com.twu.biblioteca.UserNotFoundException;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: sagar
 * Date: 27/9/13
 * Time: 10:32 AM
 * To change this template use File | Settings | File Templates.
 */
public class UserManager {
    private ArrayList<User> users=new ArrayList<User>();

   private  int libNumber=1111111;


    public UserManager(){
       users=new ArrayList<User>();
    }




    public String  addUser(String password, String emailId, Integer phoneNumber) {

        String unformatLib=Integer.toString(libNumber);
        String formatLibno=unformatLib.substring(unformatLib.length()-4,unformatLib.length());
        formatLibno=unformatLib.substring(0,unformatLib.length()-formatLibno.length())+"-"+formatLibno;
        User user=new User(formatLibno,password,emailId,phoneNumber);
        users.add(user);
        libNumber++;
        return formatLibno;
    }

    public User checkUserLogin(String userName,String password){
        User loginUser=findbyUserName(userName);

        if(loginUser.checkPassword(password))
            return loginUser;
        else
            return null;

    }

    private User findbyUserName(String userName) {
        for(User user:users){
            if(user.getUserName().equalsIgnoreCase(userName))
                return user;
        }
        return User.NON_EXISTENT_USER;
    }
}

